package com.jxc.action.spgl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VBrand;
import com.jxc.model.vo.VDiscount;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.DiscountServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/spgl")
@Action(value = "discountAction", results = { @Result(name = "discount", location = "/discount/discount.jsp"),
		@Result(name = "discountAdd", location = "/discount/discountAdd.jsp"),
		@Result(name = "discountEdit", location = "/discount/discountEdit.jsp") })
public class DiscountAction extends BaseAction implements ModelDriven<VDiscount> {
	private final static Logger logger = Logger.getLogger(DiscountAction.class);

	private VDiscount model = new VDiscount();

	@Autowired
	private DiscountServiceI discountService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	public String discount() {
		getSubAuths("SPGL_ZKGL");
		return "discount";
	}

	public String discountAdd() {
		return "discountAdd";
	}

	public String discountEdit() {
		return "discountEdit";
	}

	public void noAuth_combobox() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() != null) {
			model.setRid(vuser.getReid());
			List<VDiscount> vdiscounts = discountService.combobox(model);
			this.writeJson(vdiscounts);
		} else {
			List<VDiscount> vdiscounts = new ArrayList<VDiscount>();
			VRepertory vrepertory = new VRepertory();
			vrepertory.setUid(sessionInfo.getUserId());
			List<VRepertory> vrepertories = repertoryService.list(vrepertory);
			if (vrepertories != null && vrepertories.size() > 0) {
				for (VRepertory vre : vrepertories) {
					model.setRid(vre.getId());
					vdiscounts.addAll(discountService.combobox(model));
				}
			}
			this.writeJson(vdiscounts);
		}
	}

	public void datagrid() {
		// 查询当前用户所属仓库，将仓库添加到查询条件中
		// 如当前用户没有所属仓库，则查询经销商为当前用户的仓库，将所有仓库中的品牌查询出来
		// 如当前用户没有所属仓库，也没有哪个仓库的经销商为当前用户，则返回为空！
		if (model.getRid() != null) {
			this.writeJson(discountService.datagrid(model));
		} else {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			if (vuser.getReid() != null) {
				model.setRid(vuser.getReid());
				this.writeJson(discountService.datagrid(model));
			} else {
				VRepertory vrepertory = new VRepertory();
				vrepertory.setUid(sessionInfo.getUserId());
				List<VRepertory> vrepertories = repertoryService.list(vrepertory);
				if (vrepertories != null && vrepertories.size() > 0) {
					List<VBrand> rows = new ArrayList<VBrand>();
					Long total = 0l;
					for (VRepertory vre : vrepertories) {
						model.setRid(vre.getId());
						DataGrid subDg = discountService.datagrid(model);
						rows.addAll(subDg.getRows());
						total += subDg.getTotal();
					}
					DataGrid totalDg = new DataGrid();
					totalDg.setRows(rows);
					totalDg.setTotal(total);
					this.writeJson(totalDg);
				} else {
					DataGrid dg = new DataGrid();
					dg.setTotal(0l);
					dg.setRows(new ArrayList<VBrand>());
					this.writeJson(dg);
				}
			}
		}

	}

	/**
	 * 添加品牌
	 * 
	 */
	public void add() {
		Json j = new Json();
		try {
			discountService.save(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败，折扣已存在！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	/**
	 * 修改品牌
	 * 
	 */
	public void edit() {
		Json j = new Json();
		try {
			discountService.update(model);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败，折扣名称已存在！");
		}
		super.writeJson(j);
	}

	/**
	 * 删除品牌
	 * 
	 */
	public void delete() {
		Json j = new Json();
		discountService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

	@Override
	public VDiscount getModel() {
		return model;
	}

}
